﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using EasySL.Core;
using EasySL.Entity;
using System.Reflection;
using EasySL.Controls;
using EasySL.UI.Controls;


namespace EasySL.UI
{
    public partial class Page : UserControl
    {      
        public Page()
        {
            InitializeComponent();

            this.collapse.MouseLeftButtonUp += new MouseButtonEventHandler(collapse_MouseLeftButtonUp);

            this.menu.OnClick += new MouseButtonEventHandler(menu_OnClick);

            this.login.OnLoginSuccess += new LoginSuccess(login_OnLoginSuccess);
        }

        void login_OnLoginSuccess(string userName)
        {
            this.userInfo.Text = string.Format("Current User:  {0}, Login Time: {1}", userName, DateTime.Now);
        }
        
        void collapse_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement fe = sender as FrameworkElement;
            if (fe.Opacity == 1)
            {
                this.hideMenu.Begin();
            }
            else
            {
                this.showMenu.Begin();
            }
            
        }

        #region left menu action
        
        //When click the left menu, it'll create a right panel by menuitem's tag and reflection
        void menu_OnClick(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem element = sender as TreeViewItem;
            this.rightTitle.Text = element.Header.ToString();
            if (element.Tag != null)
            {           
                string panelName = element.Tag.ToString();

                if (panelName == "None")
                {
                    EasySL.Controls.Window.Alert("This function hasn't implemented");
                    return;
                }

                UserControl panel = this.FindName(string.Format("RightContent_{0}", panelName)) as UserControl;
                if (panel == null)
                {
                    panel = this.CreatePanel(panelName);
                    panel.Name = string.Format("RightContent_{0}", panelName);
                    this.rightContent.Children.Add(panel);
                }
                else
                {
                    this.rightContent.Children.Remove(panel);
                    this.rightContent.Children.Add(panel);
                }
            }
        }


        private UserControl CreatePanel(string panelName)
        {
            if (panelName != string.Empty)
            {
                Assembly executingAssembly = Assembly.GetExecutingAssembly();
                foreach (Type type in executingAssembly.GetTypes())
                {
                    if (type.Name.EndsWith(panelName))
                    {
                        UserControl panel = Activator.CreateInstance(type) as UserControl;
                        return panel;                        
                    }
                }
            }

            return null;
        }
        #endregion


    }
}
